package com.pachong.android.baseuicomponent.fragment;

import android.graphics.Rect;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.RecyclerView.LayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;

import com.pachong.android.baseuicomponent.LastItemMatchParentSpanSizeLookup;
import com.pachong.android.baseuicomponent.adapter.RecyclerViewBaseAdapter;


/**
 * TODO: TriggerLoadItemCount 太小了之后无法自动刷新
 */
public abstract class GridFragment extends RecyclerViewBaseFragment implements
        AdapterView.OnItemClickListener, AdapterView.OnItemLongClickListener {

    private static final String TAG = GridFragment.class.getSimpleName();
    private int mSpanCount = 2;

    public GridFragment() {
    }


    public GridFragment(int spanCount) {
        mSpanCount = spanCount;
    }


    @Override
    public LayoutManager createLayoutManager() {
        GridLayoutManager gridLayoutManager = new GridLayoutManager(getActivity(), mSpanCount);
        LastItemMatchParentSpanSizeLookup spanSizeLookup = new LastItemMatchParentSpanSizeLookup(mSpanCount);
        spanSizeLookup.setAdapter(getAdapter());
        gridLayoutManager.setSpanSizeLookup(spanSizeLookup);
        return gridLayoutManager;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = super.onCreateView(inflater, container, savedInstanceState);
        init();
        return view;
    }

    protected void init() {
        setTriggerLoadItemCount(mSpanCount * 3);
    }


    public class GridSpacingItemDecoration extends RecyclerView.ItemDecoration {

        private int mSpanCount;
        private float mVerticalSpace = -1;
        private float mHorizantalSpace = -1;
        private float mLeftEdgeSpace = -1;
        private float mRightEdgeSpace = -1;
        private float mTopEdgeSpace = -1;
        private float mBottomEdgeSpace = -1;

        public GridSpacingItemDecoration(int spanCount, float verticalSpace, float horizantalSpace, float leftEdgeSpace, float topEdgeSpace, float rightEdgeSpace, float bottomEdgeSpace) {
            this.mSpanCount = spanCount;
            this.mVerticalSpace = verticalSpace;
            this.mHorizantalSpace = horizantalSpace;
            this.mLeftEdgeSpace = leftEdgeSpace;
            this.mRightEdgeSpace = rightEdgeSpace;
            this.mTopEdgeSpace = topEdgeSpace;
            this.mBottomEdgeSpace = bottomEdgeSpace;
        }


        public GridSpacingItemDecoration(int spanCount, float verticalSpace, float horizantalSpace) {
            this(spanCount, verticalSpace, horizantalSpace, -1, -1, -1, -1);
        }


        @Override
        public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
            int position = parent.getChildAdapterPosition(view); // item position
            if (getAdapter().getItemViewType(position) == RecyclerViewBaseAdapter.ITEM_VIEW_TYPE_HEADER
                    || getAdapter().getItemViewType(position) == RecyclerViewBaseAdapter.ITEM_VIEW_TYPE_LOADING) {
                return;
            }

            if (getAdapter().hasHeaderView()) {
                position--;
            }

            int whichColumn = position % mSpanCount; // item column
            int whichRow = position / mSpanCount;
            if (getAdapter().hasHeaderView()) {
//                whichRow--; // 默认第一行为0行
            }

		/*	if (includeEdge) {
                outRect.left = spacing - whichColumn * spacing / spanCount; // spacing - column * ((1f / spanCount) * spacing)
				outRect.right = (whichColumn + 1) * spacing / spanCount; // (column + 1) * ((1f / spanCount) * spacing)

				if (position < spanCount) { // top edge
					outRect.top = spacing;
				}
				outRect.bottom = spacing; // item bottom
			} else {*/


            float left = 0, right = 0, top = 0, bottom = 0;
            //set top
            if (whichRow == 0 && mTopEdgeSpace > 0) {
                top = (int) mTopEdgeSpace;
            } else {
                if (mVerticalSpace > 0f) {
                    top += mVerticalSpace / 2;
                }
            }

            //set bottom
            if (whichRow == getAdapter().getDataCount() / mSpanCount && mBottomEdgeSpace > 0) {
                bottom = (int) mBottomEdgeSpace;
            } else {
                if (mVerticalSpace > 0f) {
                    bottom += mVerticalSpace / 2;
                }
            }


            if (isAttachLeftSize(position)) {
                //靠左边
                if (mLeftEdgeSpace > 0) {
                    left += mLeftEdgeSpace;
                }
            } else {
                if (mHorizantalSpace > 0f) {
                    left += mHorizantalSpace / 2;
                }
            }

            if (isAttachRightSize(position)) {
                //靠右边
                if (mRightEdgeSpace > 0) {
                    right += mRightEdgeSpace;
                }
            } else {

                if (mHorizantalSpace > 0f) {
                    right += mHorizantalSpace / 2;
                }
            }

            outRect.left = (int) left;
            outRect.right = (int) right;
            outRect.top = (int) top;
            outRect.bottom = (int) bottom;


			/*	outRect.left = whichColumn * spacing / spanCount; // column * ((1f / spanCount) * spacing)
                outRect.right = spacing - (whichColumn + 1) * spacing / spanCount; // spacing - (column + 1) * ((1f /    spanCount) * spacing)
				if (position >= spanCount) {
					outRect.top = spacing; // item top
				}*/


//			}
        }

        private boolean isAttachRightSize(int position) {
            return position % mSpanCount == (mSpanCount - 1);
        }

        private boolean isAttachLeftSize(int position) {
            return position % mSpanCount == 0;
        }
    }

}
